package com.csist.client;

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;

public class ClientReceiveThread implements Runnable {
	private Socket socket;
	private boolean state=true;
	private DataInputStream dis=null;

	public ClientReceiveThread(Socket socket) {
		this.socket = socket;
		try {
			dis = new DataInputStream(this.socket.getInputStream());
		} catch (IOException e) {
			close();
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		while (this.state) {
			try {
				String message = dis.readUTF();//readUTF():从数据输入流中读取用UTF-8格式编码的UniCode字符格式的字符串
				System.out.println(message);
				if("欢迎下次光临...".equals(message)){
//					close();
					break;
				}
			} catch(EOFException e){ 
				close();
			} 
			catch (IOException e) {
				close();
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 释放资源
	 */
	public void close(){
		this.state=false;
		
		if(dis!=null){
			try {
				dis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(socket!=null){
			try {
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
